<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2021/6/10
 * Time: 15:42
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Privatebanner extends Base
{
    /**
     * banner列表
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax()){
            $where['pb_status'] = ['NEQ', 3];
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('private_banner')
                ->join('up_private_setmeal', 'ps_id = pb_ps_id', 'LEFT')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('private_banner')
                ->join('up_private_setmeal', 'ps_id = pb_ps_id', 'LEFT')
                ->where($where)
                ->field('pb_id, pb_image, pb_status, pb_time, pb_update_time, ps_name')
                ->page($NowPage,$limits)
                ->order('pb_time desc')
                ->select();
            
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 新增轮播
     * @return mixed|\think\response\Json
     */
    public function bannerAdd(){
        if(Request::instance()->isPost()){
            $pb_image = input('pb_image');
            $pb_status = input('pb_status');
            $pb_ps_id = input('pb_ps_id');
            $banner = [
                'pb_image' => $pb_image,
                'pb_status' => $pb_status,
                'pb_ps_id' => $pb_ps_id,
                'pb_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_private_banner')->insert($banner);
            if($res){
                return json(['code' => 200, 'msg' => '新增成功']);
            }else{
                return json(['code' => 100, 'msg' => '新增失败']);
            }
        }
        $setMeal = Db::table('up_private_setmeal')->where('ps_status', 1)->field('ps_id, ps_name')->select();
        return $this->fetch('privatebanner/banneradd', ['setMeal' => $setMeal]);
    }
    
    /**
     * 编辑轮播
     * @return mixed|\think\response\Json
     */
    public function bannerEdit(){
        $pb_id = input('pb_id');
        if(Request::instance()->isPost()){
            $pb_status = input('pb_status');
            $pb_image = input('pb_image');
            $pb_ps_id = input('pb_ps_id');
            $update = [
                'pb_status' => $pb_status,
                'pb_ps_id' => $pb_ps_id,
                'pb_update_time' => date('Y-m-d H:i:s')
            ];
            if($pb_image) $update['pb_image'] = $pb_image;
            
            $res = Db::table('up_private_banner')->where('pb_id', $pb_id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $banner = Db::table('up_private_banner')->where('pb_id', $pb_id)->field('pb_image, pb_status, pb_ps_id')->find();
        $setMeal = Db::table('up_private_setmeal')->where('ps_status', 1)->field('ps_id, ps_name')->select();
        $this->assign(['pb_id' => $pb_id, 'banner' => $banner, 'setMeal' => $setMeal]);
        return $this->fetch('privatebanner/banneredit');
    }
    
    /**
     * 下架
     * @return \think\response\Json
     */
    public function bannerDown(){
        $pb_id = input('id');
        $res = Db::table('up_private_banner')->where('pb_id', $pb_id)->setField('pb_status', 2);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 上架
     * @return \think\response\Json
     */
    public function bannerOn(){
        $pb_id = input('id');
        $res = Db::table('up_private_banner')->where('pb_id', $pb_id)->setField('pb_status', 1);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 删除轮播图
     * @return \think\response\Json
     */
    public function bannerDel(){
        $pb_id = input('id');
        $res = Db::table('up_private_banner')->where('pb_id', $pb_id)->setField('pb_status', 3);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
}